import { tableSorter } from '@/utils';
import { Button, Tooltip } from 'antd';
import pageStyles from '@/common/pageLayout.less';
import { EyeOutlined } from '@ant-design/icons';
import moment from 'moment';

/**
 * 返回表格配置
 * @param taskType true:待办   false:已办
 * @param onOptClick onOptClick?.onView(record),跳转路由操作
 * @param isTaskView true:任务管理页   false:采购门户页
 * @returns
 */
const getHomeTableColumns = (taskType: boolean, onOptClick: any, isTaskView: boolean) => {
  let columns: Record<string, any>[] = [
    {
      title: '标题',
      dataIndex: 'subject',
      key: 'subject',
      width: 160,
      visible: 'allShow',
      fixed: 'left',
      sorter: (a: any, b: any) => (a.subject > b?.subject ? 1 : -1),
      render: (text: string | null, record: Record<string, any>) => {
        return (
          <Tooltip title={text}>
            <span className={pageStyles['over-flow-hidden']}>
              <a
                onClick={() => {
                  onOptClick?.onView(record);
                }}
              >
                {text ? text : '--'}
              </a>
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '类型',
      dataIndex: 'defName',
      key: 'defName',
      width: 100,
      visible: 'allShow',
      sorter: (a: any, b: any) => (a?.defName > b?.defName ? 1 : -1),
      render: (text: string | null, record: Record<string, any>) => {
        return (
          <Tooltip title={text}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '当前环节',
      dataIndex: 'currentStage',
      key: 'currentStage',
      visible: '0',
      width: 170,
      sorter: (a: any, b: any) => (a?.nodeName > b?.nodeName ? 1 : -1),
      render: (text: string | null, record: Record<string, any>) => {
        return (
          <Tooltip title={text}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '时间',
      dataIndex: 'createDateTime',
      key: 'createDateTime',
      width: 160,
      visible: '0',
      sorter: (a: any, b: any) => tableSorter(a, b, 'createDateTime'),
      render: (text: string, record: Record<string, any>) => {
        return (
          <Tooltip title={text ? text : ''}>
            <span className={pageStyles['over-flow-hidden']}>{text ? moment(text).format('YYYY-MM-DD HH:mm') : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '办理时间',
      dataIndex: 'endTime',
      key: 'endTime',
      width: 160,
      visible: '2',
      sorter: (a: any, b: any) => tableSorter(a, b, 'endTime'),
      render: (text: string, record: Record<string, any>) => {
        return (
          <Tooltip title={text ? text : ''}>
            <span className={pageStyles['over-flow-hidden']}>{text ? moment(text).format('YYYY-MM-DD HH:mm') : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '发送人',
      dataIndex: 'creatorName',
      key: 'creatorName',
      width: 100,
      visible: '0',
      sorter: (a: any, b: any) => (a?.creatorName > b?.creatorName ? 1 : -1),
      render: (text: string, record: Record<string, any>) => {
        return (
          <Tooltip title={text ? text : ''}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '状态',
      dataIndex: 'isRemark',
      key: 'isRemark',
      width: 100,
      visible: '2',
      sorter: (a: any, b: any) => (a?.isRemark > b?.isRemark ? 1 : -1),
      render: (text: string) => {
        let showText = '';
        if (text === '0') {
          showText = '待办';
        } else if (text === '2') {
          showText = '已办';
        } else if (text === '4') {
          showText = '办结';
        } else if (text === '8') {
          showText = '抄送';
        }
        return showText;
      },
    },
    {
      title: '操作',
      dataIndex: 'options',
      key: 'options',
      width: 100,
      visible: 'allShow',
      isTaskView: 'true',
      render: (_: any, record: Record<string, any>) => {
        return (
          <div style={{ display: 'flex' }}>
            <Button
              onClick={() => {
                onOptClick?.onView(record);
              }}
              type={'link'}
              size={'small'}
              icon={<EyeOutlined />}
            >
              查看
            </Button>
          </div>
        );
      },
    },
  ];

  columns = columns?.filter((item: any) => (taskType ? item?.visible != '2' : item?.visible != '0'));

  if (!isTaskView) {
    columns = columns?.filter((item: any) => item?.isTaskView != 'true');
  }

  return columns;
};

export { getHomeTableColumns };
